package com.yfqy.admin.conf.redis;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedisConfig {

    @Autowired
    private RedissonProperties redissonProperties;

    /**
     * redis 单机模式
     *
     * @return
     */
    @Bean(name = "singleRedisClient")
    @ConditionalOnProperty(name = "admin-redis.basics.model", havingValue = "single")
    public RedissonClient singleClient() {
        Config config = new Config();
        SingleServerConfig singleServer = config.useSingleServer();
        singleServer.setAddress(redissonProperties.getSingle().getAddress())
                .setKeepAlive(true)
                .setConnectionMinimumIdleSize(redissonProperties.getBasics().getConnectionMinimumIdleSize())
                .setDatabase(redissonProperties.getBasics().getConnectionPoolSize())
                .setTimeout(redissonProperties.getBasics().getTimeout())
                .setDatabase(redissonProperties.getSingle().getDatabase());
        if (StringUtils.isNotBlank(redissonProperties.getBasics().getPassword())) {
            singleServer.setPassword(redissonProperties.getBasics().getPassword());
        }
        // Key 和 Value 都使用 String 序列化（默认是 JsonJacksonCodec）
        config.setCodec(new StringCodec());
        return Redisson.create(config);
    }

    /**
     * redis 集群模式
     *
     * @return
     */
    @Bean(name = "clusterRedisClient")
    @ConditionalOnProperty(name = "admin-redis.basics.model", havingValue = "cluster")
    public RedissonClient clusterClient() {
        Config config = new Config();
        ClusterServersConfig clusterServers = config.useClusterServers();
        clusterServers
                .setScanInterval(2000)
                .setKeepAlive(true)
                .setMasterConnectionPoolSize(redissonProperties.getBasics().getConnectionPoolSize())
                .setMasterConnectionMinimumIdleSize(redissonProperties.getBasics().getConnectionMinimumIdleSize())
                .setSlaveConnectionPoolSize(redissonProperties.getBasics().getConnectionPoolSize())
                .setSlaveConnectionMinimumIdleSize(redissonProperties.getBasics().getConnectionMinimumIdleSize())
                .setReadMode(ReadMode.MASTER_SLAVE)
                .setPingConnectionInterval(redissonProperties.getBasics().getTimeout());
        if (StringUtils.isNotBlank(redissonProperties.getBasics().getPassword())) {
            clusterServers.setPassword(redissonProperties.getBasics().getPassword());
        }
        for (String address : redissonProperties.getCluster().getAddress()) {
            clusterServers.addNodeAddress(address);
        }
        config.setCodec(new StringCodec());
        return Redisson.create(config);
    }
}
